package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.Permission;
import com.yili.oa.service.PermisService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.vo.request.PermissionDataReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/3/27 0027
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Controller
@RequestMapping("/sys/permission")
public class PermissionApi {

    @Autowired
    private PermisService service;

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/addButton")
    public String addButton(){
        return "view/user/permissionAdd";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @GetMapping("/editButton/{permissionId}")
    public String editButton(@PathVariable("permissionId") String permissionId, ModelMap modelAndView){
        if (StringUtils.isBlank(permissionId)){
            throw new AjaxException("该权限不存在");
        }
        modelAndView.put("info", service.getById(permissionId));
        return "view/user/permissionAdd";
    }
    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public TableBase list(SearchBase search){
        if (search==null){
            search = new SearchBase();
        }
        PageHelper.startPage(search.getPage(),search.getLimit());
        List<Permission> list = service.getList(search);
        PageInfo pageInfo = new PageInfo<>(list,search.getLimit());
        TableBase tableBase  = new TableBase();
        tableBase.setData(list);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    /**
     * 添加权限
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public String add(PermissionDataReq permissionDataReq){
        if (StringUtils.isBlank(permissionDataReq.getPermissionId())) {
            //名字查重
            Boolean result = service.checkName(permissionDataReq.getPermissionName(),null);
            if (!result){
                throw new AjaxException("该权限已存在");
            }
            //code查重
            Boolean result2 = service.checkCode(permissionDataReq.getPermissionKey(),null);
            if (!result2){
                throw new AjaxException("该code已存在");
            }
            service.add(permissionDataReq);
        }else {
            Boolean result = service.checkName(permissionDataReq.getPermissionName(),
                    permissionDataReq.getPermissionId());
            if (!result){
                throw new AjaxException("该权限已存在");
            }
            Boolean result2 = service.checkCode(permissionDataReq.getPermissionKey(),
                    permissionDataReq.getPermissionId());
            if (!result2){
                throw new AjaxException("该code已存在");
            }
            service.update(permissionDataReq);
        }
        return "success";

    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,String> edit(@RequestBody String[] permissionIds) {
        if (permissionIds==null){
            throw new AjaxException("请选择要删除的数据");
        }
        Map<String, String> map = new HashMap<>();
        List<String> list = Arrays.asList(permissionIds);
        service.del(list);
        map.put("success", "1");
        return map;
    }
}
